<template>
  <!-- 修改模块 -->
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="true"
    :visible.sync="insideFlage"
    @close="patchModelFormConfig.cancel()"
    class="vc-dialog-form"
    destroy-on-close
    title="修改模块"
    width="480px"
  >
    <supForm :formConfig="patchModelFormConfig" ref="patchModel" />
    <div class="dialog-footer" slot="footer">
      <el-button size="small" @click="patchModelFormConfig.cancel()">取消</el-button>
      <el-button size="small" :disabled="disabled" @click="patchModelFormConfig.ok()" type="primary">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import supForm from '_c/form'
export default {
  name: 'patchModel',
  components: { supForm },
  props: {
    patchModel: {
      type: Boolean,
      default () {
        return false
      }
    },
    selectNode: {
      type: Object,
      default () {
        return {}
      }
    },
    rgTreeDataHasTop: {
      type: Array,
      default () {
        return []
      }
    }
  },
  data () {
    return {
      insideFlage: false,
      disabled: false,
      // 修改模块
      patchModelFormConfig: {
        items: [
          {
            title: '上级模块',
            key: 'pid',
            type: 'selectTree',
            defaultProps: {
              id: 'id',
              label: 'text',
              children: 'children',
              isLeaf: 'isLeaf'
            },
            group: [],
            expandedKeys: []
          },
          {
            title: '名称',
            maxlength: 20,
            key: 'name',
            required: true,
            message: '请输入名称'
          }
        ],
        params: {
          pid: null
        },
        cancel: () => {
          this.$refs.patchModel.resetForm()
          this.insideFlage = false
          this.$emit('close', false)
        },
        ok: () => {
          let flage = this.$refs.patchModel.save()
          if (flage) {
            this.disabled = true
            this.$api
              .patchModel({
                data: this.patchModelFormConfig.params
              })
              .then(res => {
                this.insideFlage = false
                if (res.success) {
                  this.$refs.patchModel.resetForm()
                  this.$message.success('修改成功')
                  this.$emit('close', res)
                } else {
                  this.disabled = false
                }
              })
              .catch(err => {
                this.disabled = false
              })
          }
        }
      }
    }
  },
  computed: {},
  watch: {
    patchModel (value) {
      if (value) {
        this.init(value)
      }
    }
  },
  methods: {
    init (value) {
      this.insideFlage = value
      this.disabled = false
      this.patchModelFormConfig.items[0].group = this.rgTreeDataHasTop
      this.patchModelFormConfig.params = this.selectNode.data
    }
  }
}
</script>
